//小时分钟格式转换为分钟数
 function timeToMinutes (str) {
  var arrminutes = str.split(":");
  if (arrminutes.length > 1) {
    var minutes = parseInt(arrminutes[0]) * 60 + parseInt(arrminutes[1]);
    return minutes;
  }
  else {
    return 0;
  }
}

//分钟数转换为小时分钟格式
function minutesToTime(minutes) {
  return ('0' + parseInt(minutes / 60).toString()).slice(-2) + ':' + ('0' + (minutes % 60).toString()).slice(-2);
}

//字节数组转十六进制字符串，对负值填坑
function Bytes2HexString(arrBytes) {
  var str = "";
  for (var i = 0; i < arrBytes.length; i++) {
    var tmp;
    var num = arrBytes[i];
    if (num < 0) {
      //此处填坑，当byte因为符合位导致数值为负时候，需要对数据进行处理
      tmp = (255 + num + 1).toString(16);
    } else {
      tmp = num.toString(16);
    }
    if (tmp.length == 1) {
      tmp = "0" + tmp;
    }
    str += (' ' + tmp);
  }
  return str;
}

/**
 * 十六进制的字符串(如："6E")，转换成十六进制的数组类型
 * @param {*} str 
 */
function hex_to_bytes(str) {
  var len = str.length;
  if (len & 1) {
    str = '0' + str;
    len++;
  }
  var bytes = new Uint8Array(len >> 1);
  for (var i = 0; i < len; i += 2) {
    bytes[i >> 1] = parseInt(str.substr(i, 2), 16);
  }
  return bytes;
}
//广播数据为Array Buffer类型   ArrayBuffer转16进度字符
function ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function (bit) {
      return ' ' + ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('');
}


module.exports = {
  minutesToTime: minutesToTime,
  timeToMinutes: timeToMinutes,
  ab2hex: ab2hex,
  hex_to_bytes: hex_to_bytes,
  Bytes2HexString:Bytes2HexString
};